#include<bits/stdc++.h>
#define int long long
#define fff(i,l,r) for(int i=l;i<=r;i++)
#define sfff(i,l,r,j) for(int i=l;i<=r;i+=j)
bool pd(int x) {return (x&(x-1))==0?1:0;}
using namespace std;
const int N=1e5+5;
int n,m;
int a[N],c[N];
vector<int> d[N];
int got(int x){
    while(!pd(x))x++;
    return x;
}
int x0x,x1x,x2x,x3x;
int hv(int i){
    if(i==0)return x0x;
    if(i==1)return x1x;
    if(i==2)return x2x;
    return x3x;
}
int chge(int i){return a[i] xor hv(i%4);}
void solve(){
    cin>>x0x>>x1x>>x2x>>x3x;
    int returnn=0;
    fff(qwerty,1,m){
        fff(i,1,n)a[i]=chge(i);
        int ans=0,tn=n;
        while(!pd(tn)){
            tn++;
            ans+=tn;
        }
        ans-=tn;
        int ttn=tn,k=0;
        while(ttn){
            k++;
            ttn/=2;
        }
        fff(i,n+1,k)a[i]=-1;
        vector<int> aa,bb;
        fff(i,1,k)bb.push_back(i);
        sfff(i,1,k,2){
            aa.clear();
            sfff(j,0,signed(pow(2,i))-1,2){
                int a1=bb[j],a2=bb[j+1];
                if(d[i][j/2]==0){
                    if(a[a1]>=i)aa.push_back(a1);
                    else aa.push_back(a2);
                }
                else{
                    if(a[a2]>i)aa.push_back(a2);
                    else aa.push_back(a1);
                }
            }
            bb.clear();
            sfff(j,0,signed(pow(2,i+1))-1,2){
                int a1=aa[j],a2=aa[j+1];
                if(d[i][j/2]==0){
                    if(a[a1]>=i)bb.push_back(a1);
                    else bb.push_back(a2);
                }
                else{
                    if(a[a2]>=i)bb.push_back(a2);
                    else bb.push_back(a1);
                }
            }
        }
        ans+=a[0];   
        returnn ^=ans;
    }
    cout<<returnn<<'\n';
}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    fff(i,1,n){
        cin>>a[i];
    }
    int tn=got(n);
    fff(i,1,m){
        cin>>c[i];
    }
    sort(c+1,c+n+1);
    int k=0;
    while(tn!=1)k++,tn/=2;
    while(k!=0){
        int ti=1;
        fff(i,1,k){
            char ch;
            cin>>ch;
            d[ti].push_back(ch-'0');
        }
        ti++;
        k/=2;
    }
    int T;
    cin>>T;
    while(T--)solve();
    return 0;
}